﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Engine.Classes.*;
    import Engine.Managers.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import Display.FactoryUI.*;
    import Classes.orders.*;
    import Modules.franchise.data.*;
    import Display.LotUI.*;
    import Classes.MiniQuest.*;
    import Modules.stats.types.*;
    import Transactions.lot.*;

    public class LotManager implements IGameWorldStateObserver {

        protected var m_world:GameWorld;
        protected var m_lotSites:Array;
        protected var m_sentPendingOrders:Array;
        protected var m_receivedPendingOrders:Array;
        protected var m_pickGroupDictionary:Dictionary;
        protected var m_isInitialized:Boolean = false;

        public function LotManager(_arg1:GameWorld){
            this.m_world = _arg1;
            this.m_world.addObserver(this);
        }
        public function get sentPendingOrders():Array{
            return (this.m_sentPendingOrders);
        }
        public function get receivedPendingOrders():Array{
            return (this.m_receivedPendingOrders);
        }
        public function get lotSites():Array{
            return (this.m_lotSites);
        }
        public function initLots():void{
            this.cleanUp();
            this.completeAcceptedOrders();
            this.m_lotSites = Global.world.getObjectsByTypes([WorldObjectTypes.LOT_SITE]);
            this.m_sentPendingOrders = Global.world.orderMgr.getOrders(OrderType.LOT, OrderStatus.SENT, OrderStates.PENDING);
            this.m_receivedPendingOrders = Global.world.orderMgr.getOrders(OrderType.LOT, OrderStatus.RECEIVED, OrderStates.PENDING);
            this.m_pickGroupDictionary = new Dictionary(true);
            this.drawView();
            this.m_isInitialized = true;
        }
        public function placeOrder(_arg1:String, _arg2:int, _arg3:String):void{
            var _local4:String;
            var _local9:MapResource;
            var _local5:Item = Global.gameSettings.getItemByName(_arg3);
            if (_local5.type == "business"){
                _local4 = Global.franchiseManager.getFranchiseName(_local5.name, Global.player.uid);
            } else {
                _local4 = "";
            };
            var _local6:TLotOrderPlace = new TLotOrderPlace(_arg1, _arg2, _arg3, _local4, 0, this.placeOrderCompleteHandler);
            GameTransactionManager.addTransaction(_local6);
            var _local7:LotOrder = new LotOrder(_arg1, Global.player.uid, _arg2, _arg3, _local4, 0, OrderStatus.SENT);
            this.m_sentPendingOrders[this.m_sentPendingOrders.length] = _local7;
            var _local8:WorldObject = Global.world.getObjectById(_arg2);
            if (_local8){
                _local9 = this.replaceLotSite(_arg2, _arg3, _local4, Global.player.uid);
                _local9.itemOwner = Global.player.uid;
                _local9.isPendingOrder = true;
                Global.world.citySim.roadManager.updateRoads(_local9);
            };
            if (Global.franchiseManager.isFranchiseOrder(_local7)){
                StatsManager.social(StatsCounterType.FRANCHISES, _arg1, "place_pending_franchise", _local7.getResourceType());
            };
        }
        public function checkLotOrderAvailablity():Boolean{
            var _local3:LotOrder;
            var _local4:String;
            var _local1:Boolean;
            var _local2:String = Global.world.ownerId;
            for each (_local3 in this.m_sentPendingOrders) {
                _local4 = _local3.getRecipientID();
                if (_local4 == _local2){
                    _local1 = false;
                    break;
                };
            };
            return (_local1);
        }
        public function accepted(_arg1:LotOrder):MapResource{
            var _local3:Vector3;
            var _local4:MapResource;
            var _local5:TLotOrderAccept;
            var _local2:int = (this.m_receivedPendingOrders) ? this.m_receivedPendingOrders.indexOf(_arg1) : -1;
            if (_local2 >= 0){
                _local3 = new Vector3(0, 0);
                _local4 = this.replaceLotSite(_arg1.getLotId(), _arg1.getResourceType(), _arg1.getOrderResourceName(), _arg1.getSenderID(), _local3);
                _arg1.setXOffset(_local3.x);
                _arg1.setYOffset(_local3.y);
                _local5 = new TLotOrderAccept(_arg1.getSenderID(), _arg1.getLotId());
                _arg1.setState(OrderStates.ACCEPTED);
                Global.world.orderMgr.removeOrder(_arg1);
                GameTransactionManager.addTransaction(_local5);
                this.m_receivedPendingOrders.splice(this.m_receivedPendingOrders.indexOf(_arg1), 1);
                this.redrawView();
                Global.world.citySim.roadManager.updateRoads(_local4);
                if (Global.franchiseManager.isFranchiseOrder(_arg1)){
                    StatsManager.social(StatsCounterType.FRANCHISES, _arg1.getSenderID(), "accept_franchise");
                };
                if (this.m_receivedPendingOrders.length == 0){
                    Global.world.citySim.miniQuestManager.endMiniQuest(PendingFranchiseMQ.QUEST_NAME);
                };
                return (_local4);
            };
            return (null);
        }
        public function later(_arg1:LotOrder):void{
            var _local4:DisplayObject;
            var _local5:SlidePickGroup;
            var _local6:SlidePick;
            var _local2:int = _arg1.getLotId();
            var _local3:LotSite = this.getLotSite(_local2);
            if (_local3){
                _local4 = _local3.getPick();
                if ((_local4 is SlidePickGroup)){
                    _local5 = (_local4 as SlidePickGroup);
                    _local6 = _local5.getPickByPropValue("order", _arg1);
                    if (_local6){
                        _local5.removePick(_local6);
                        _local6 = this.createLotSlidePick(_arg1);
                        _local5.addPick(_local6);
                        _local5.redraw();
                    };
                };
            };
        }
        public function declined(_arg1:LotOrder):void{
            var _local3:TLotOrderDeny;
            var _local2:int = (this.m_receivedPendingOrders) ? this.m_receivedPendingOrders.indexOf(_arg1) : -1;
            if (_local2 >= 0){
                _local3 = new TLotOrderDeny(_arg1);
                GameTransactionManager.addTransaction(_local3);
                this.m_receivedPendingOrders.splice(this.m_receivedPendingOrders.indexOf(_arg1), 1);
                if (Global.franchiseManager.isFranchiseOrder(_arg1)){
                    StatsManager.social(StatsCounterType.FRANCHISES, _arg1.getSenderID(), "decline_franchise");
                };
                if (this.m_receivedPendingOrders.length == 0){
                    Global.world.citySim.miniQuestManager.endMiniQuest(PendingFranchiseMQ.QUEST_NAME);
                };
            };
            this.removePick(_arg1);
        }
        public function cancel(_arg1:LotOrder):void{
            var _local3:TLotOrderCancel;
            var _local4:Item;
            var _local2:int = (this.m_sentPendingOrders) ? this.m_sentPendingOrders.indexOf(_arg1) : -1;
            if (_local2 >= 0){
                _local3 = new TLotOrderCancel(_arg1);
                GameTransactionManager.addTransaction(_local3);
                this.m_sentPendingOrders.splice(this.m_sentPendingOrders.indexOf(_arg1), 1);
                if (Global.franchiseManager.isFranchiseOrder(_arg1)){
                    StatsManager.social(StatsCounterType.FRANCHISES, _arg1.getSenderID(), _arg1.getRecipientID(), "cancel_franchise");
                };
                _local4 = Global.gameSettings.getItemByName(_arg1.getResourceType());
                Global.player.gold = (Global.player.gold + _local4.cost);
                this.returnLotSite(_arg1.getLotId(), _arg1.getResourceType(), _arg1.getSenderID());
                this.redrawView();
            };
        }
        protected function removePick(_arg1:LotOrder):void{
            var _local4:DisplayObject;
            var _local5:SlidePickGroup;
            var _local6:SlidePick;
            var _local2:int = _arg1.getLotId();
            var _local3:LotSite = this.getLotSite(_local2);
            if (_local3){
                _local4 = _local3.getPick();
                if ((_local4 is SlidePickGroup)){
                    _local5 = (_local4 as SlidePickGroup);
                    _local6 = _local5.getPickByPropValue("order", _arg1);
                    if (_local6){
                        _local5.removePick(_local6);
                    };
                    if (_local5.numPicks <= 0){
                        _local3.removePick();
                        this.m_pickGroupDictionary[_local3] = null;
                    } else {
                        _local5.redraw();
                    };
                };
            };
        }
        public function initialize():void{
        }
        public function cleanUp():void{
            var _local1:Object;
            var _local2:SlidePickGroup;
            for (_local1 in this.m_pickGroupDictionary) {
                _local2 = this.m_pickGroupDictionary[_local1];
                if (_local2){
                    _local2.cleanUp();
                };
                delete this.m_pickGroupDictionary[_local1];
            };
            this.m_pickGroupDictionary = new Dictionary(true);
        }
        public function onGameLoaded(_arg1:Object):void{
            if (this.m_isInitialized){
                this.redrawView();
            };
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function removeLotSite(_arg1:LotSite):void{
            var _local2:int = this.m_lotSites.indexOf(_arg1);
            if (_local2 >= 0){
                this.m_lotSites.splice(_local2, 1);
            };
            _arg1.remove();
            this.cleanUp();
        }
        public function addLotSite(_arg1:LotSite):void{
            if (((!(this.m_lotSites)) || ((this.m_lotSites.length <= 0)))){
                this.cleanUp();
                this.redrawView();
            } else {
                if (this.m_lotSites){
                    this.m_lotSites[this.m_lotSites.length] = _arg1;
                };
            };
        }
        private function adjustInLotForRoads(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3, _arg4:Vector3=null):void{
            var _local6:int;
            var _local7:int;
            var _local5:Vector3 = _arg1.getSize();
            Global.world.citySim.roadManager.updateResource(_arg1);
            if (_arg4){
                _arg4.x = 0;
                _arg4.y = 0;
            };
            if (!_arg1.isAdjacentToAnyRoad){
                _local6 = 0;
                while (_local6 <= (_arg3.x - _local5.x)) {
                    _local7 = 0;
                    while (_local7 <= (_arg3.y - _local5.y)) {
                        _arg1.setPosition((_arg2.x + _local6), (_arg2.y + _local7), _arg2.z);
                        Global.world.citySim.roadManager.updateResource(_arg1);
                        if (_arg1.isAdjacentToAnyRoad){
                            if (_arg4){
                                _arg4.x = _local6;
                                _arg4.y = _local7;
                            };
                            break;
                        };
                        _local7++;
                    };
                    _local6++;
                };
            };
        }
        public function replaceLotSite(_arg1:Number, _arg2:String, _arg3:String="", _arg4:String="", _arg5:Vector3=null):MapResource{
            var _local12:MapResource;
            var _local13:Class;
            var _local6:Boolean;
            var _local7:LotSite = this.getLotSite(_arg1);
            var _local8:Vector3 = _local7.getPosition();
            var _local9:int = _local7.getDirection();
            var _local10:Vector3 = _local7.getSize();
            var _local11:Item = Global.gameSettings.getItemByName(_arg2);
            if (((!((_local11.construction == null))) && (_local6))){
                _local12 = ConstructionSite.createConstructionSite(_arg2);
            } else {
                _local13 = ItemClassDefinitions.getClassByItem(_local11);
                _local12 = new _local13(_local11.name);
            };
            _local12.itemOwner = _arg4;
            _local12.setOuter(Global.world);
            _local12.onObjectDrag(_local8);
            _local12.setPosition(_local8.x, _local8.y, _local8.z);
            _local12.rotateToDirection(_local9);
            this.adjustInLotForRoads(_local12, _local8, _local10, _arg5);
            _local12.setId(_arg1);
            _local12.conditionallyReattach(true);
            _local12.onBuildingConstructionCompleted_PreServerUpdate();
            if (_arg3){
                _local12.initFriendData(_arg3);
            };
            this.removeLotSite(_local7);
            return (_local12);
        }
        public function returnLotSite(_arg1:Number, _arg2:String, _arg3:String=""):void{
            var _local4:Business = Global.franchiseManager.getPendingFranchiseFromTypeAndOwner(_arg2, _arg3);
            var _local5:Vector3 = _local4.getPosition();
            _local4.detach();
            var _local6:LotSite = new LotSite(LotSite.LOT_NAME);
            _local6.setId(_arg1);
            _local6.setOuter(Global.world);
            _local6.onObjectDrag(_local5);
            _local6.setPosition(_local5.x, _local5.y, _local5.z);
            this.adjustInLotForRoads(_local6, _local6.getPosition(), _local6.getSize());
            _local6.conditionallyReattach(true);
            this.addLotSite(_local6);
        }
        protected function completeAcceptedOrders():void{
            var _local2:LotOrder;
            var _local3:TLotOrderComplete;
            var _local1:Array = Global.world.orderMgr.getOrders(OrderType.LOT, OrderStatus.SENT, OrderStates.ACCEPTED);
            for each (_local2 in _local1) {
                _local3 = new TLotOrderComplete(_local2);
                GameTransactionManager.addTransaction(_local3);
            };
        }
        protected function drawView():void{
            if (Global.isVisiting()){
                this.drawVisitView();
            } else {
                this.drawHomeView();
            };
        }
        protected function redrawView():void{
            this.m_lotSites = Global.world.getObjectsByTypes([WorldObjectTypes.LOT_SITE]);
            this.drawView();
        }
        protected function drawVisitView():void{
            var _local1:LotSite;
            var _local2:Number;
            var _local3:MapResource;
            var _local5:LotSlidePick;
            var _local6:SlidePickGroup;
            var _local7:LotOrder;
            var _local8:Vector.<FranchiseExpansionData>;
            var _local9:FranchiseExpansionData;
            var _local10:Boolean;
            var _local11:String;
            if (Global.world.isOwnerCitySam){
                _local8 = Global.franchiseManager.model.getFranchisesByLocation(GameWorld.CITY_SAM_OWNER_ID);
                if (_local8.length){
                    _local9 = _local8[0];
                    for each (_local1 in this.m_lotSites) {
                        _local10 = Boolean((_local1.targetType == "business"));
                        if (_local10){
                            _local3 = this.replaceLotSite(_local1.getId(), _local9.franchiseType, "", Global.player.uid);
                            if (_local3){
                                Global.world.citySim.roadManager.updateRoads(_local3);
                            };
                            break;
                        };
                    };
                };
                return;
            };
            var _local4:Object = {};
            for each (_local1 in this.m_lotSites) {
                _local2 = _local1.getId();
                if (_local2 > 0){
                    _local4[_local2] = _local1;
                };
            };
            for each (_local7 in this.m_sentPendingOrders) {
                _local11 = _local7.getRecipientID();
                if (_local11 == Global.world.ownerId){
                    _local2 = _local7.getLotId();
                    _local1 = _local4[_local2];
                    if (((!(_local1)) && ((this.m_lotSites.length > 0)))){
                        _local1 = this.getDefaultLotSite();
                    };
                    if (_local1){
                        _local3 = this.replaceLotSite(_local1.getId(), _local7.getResourceType(), _local7.getOrderResourceName(), _local7.getSenderID());
                        _local3.itemOwner = _local7.getSenderID();
                        _local3.isPendingOrder = true;
                    };
                };
            };
            if (_local3){
                Global.world.citySim.roadManager.updateRoads(_local3);
            };
        }
        public function getDefaultLotSite():LotSite{
            var _local1:LotSite;
            if (((this.m_lotSites) && ((this.m_lotSites.length > 0)))){
                _local1 = this.m_lotSites[0];
            };
            return (_local1);
        }
        protected function drawHomeView():void{
            var _local3:LotSite;
            var _local4:Number;
            var _local5:LotSlidePick;
            var _local6:SlidePickGroup;
            var _local7:LotOrder;
            var _local8:Object;
            var _local9:LotSite;
            var _local1:int;
            while (_local1 < this.m_lotSites.length) {
                _local9 = this.m_lotSites[_local1];
                if (_local9.getId() == 0){
                    this.m_lotSites.splice(_local1, 1);
                    _local1--;
                };
                _local1++;
            };
            if (((((((!(this.m_lotSites)) && ((this.m_lotSites.length <= 0)))) && (!(this.m_receivedPendingOrders)))) && ((this.m_receivedPendingOrders.length <= 0)))){
                return;
            };
            var _local2:Object = {};
            for each (_local3 in this.m_lotSites) {
                _local4 = _local3.getId();
                if (_local4 > 0){
                    _local2[_local4] = _local3;
                };
            };
            for each (_local7 in this.m_receivedPendingOrders) {
                _local4 = _local7.getLotId();
                _local3 = ((_local2[_local4]) || (this.m_lotSites[0]));
                if (_local3){
                    _local6 = this.m_pickGroupDictionary[_local3];
                    if (!_local6){
                        _local6 = new SlidePickGroup(this);
                        this.m_pickGroupDictionary[_local3] = _local6;
                    };
                    _local7.setLotId(_local3.getId());
                    _local5 = this.createLotSlidePick(_local7);
                    _local6.addPick(_local5);
                };
            };
            for (_local8 in this.m_pickGroupDictionary) {
                _local6 = this.m_pickGroupDictionary[_local8];
                _local6.init();
                _local3 = (_local8 as LotSite);
                _local3.setPick(_local6);
            };
        }
        protected function createLotSlidePick(_arg1:LotOrder):LotSlidePick{
            var _local2:LotSlidePick = new LotSlidePick(this, _arg1, true);
            return (_local2);
        }
        public function getLotSite(_arg1:Number):LotSite{
            var _local2:LotSite;
            var _local3:LotSite;
            for each (_local3 in this.m_lotSites) {
                if (_local3.getId() == _arg1){
                    _local2 = _local3;
                    break;
                };
            };
            return (_local2);
        }
        private function placeOrderCompleteHandler(_arg1:Object):void{
        }

    }
}//package Classes.sim 
